import smtplib
import csv
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
from email.header import Header

# 邮件内容
text = '''一个人的一生只有三天，昨天，今天和明天。
昨天已经过去永不复返，我们要及时汲取昨天的经验和教训；
今天已经和我们在一起，我们要倍加珍惜，脚踏实地做事情；
明天将要到来，我们要提前规划，仰望星空！
'''
# 接收人姓名邮箱数据
data = [['wangcheng1','1622579887@qq.com'],['wangcheng','1041575391@qq.com']]

#写入收件人数据
with open('D://to_addrs.csv','w',newline='') as f:
      writer = csv.writer(f)
      for row in data:
            writer.writerow(row)

# 发信方的信息：发送邮件，授权码
from_addr = '1041575391@qq.com'
password = 'osmoycuqfgvobddd'

# 收信方邮箱
to_addr = '1041575391@qq.com'

#发信服务器
smtp_server = 'smtp.qq.com'

# 读取收件人数据，并启动写信和发信流程
with open('D://to_addrs.csv','r') as f:
      reader = csv.reader(f)
      for row in reader:
            to_addrs=row[1]
            msg = MIMEText(text,'plain','utf-8')
            msg['From'] = formataddr((str(Header('冉成科技','utf-8')),'1041575391@qq.com'))
            msg['To'] = Header(to_addrs)
            msg['Subject'] = Header('测试Python发送邮件','utf-8')
            #开启发信服务，这里使用加密传输
            server = smtplib.SMTP()
            server.connect(smtp_server,25)
            server.login(from_addr,password)
            try:
                  server.sendmail(from_addr,to_addrs,msg.as_string())
                  print('恭喜，发送成功')
            except:
                  print('发送是被，请重试')


# 关闭服务器
server.quit()